package com.cooper.service.seller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cooper.core.JsonResult;
import com.cooper.dao.seller.ShopStatDAO;
import com.cooper.entity.LikeDetailEntity;
import com.cooper.entity.ShopStatEntity;
import com.cooper.entity.UserInfo;
import com.cooper.service.item.LikeDetailService;
import com.cooper.util.Controllers;

@Service
public class ShopStatService {
	
	protected Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private LikeDetailService likeDetailService;
	
	@Autowired
	private ShopStatDAO shopStatDao;
	
	public Long saveEntity(ShopStatEntity entity) throws Exception{
		return this.shopStatDao.saveEntity(entity);
	}
	
	public JsonResult like(LikeDetailEntity likeDetail, UserInfo userInfo,String origin) throws Exception{
		JsonResult json = new JsonResult();
		ShopStatEntity shopStat = shopStatDao.loadBySellerId(likeDetail.getOuterId());
		if(shopStat==null){
			shopStat = new ShopStatEntity();
			shopStat.setSellerId(likeDetail.getOuterId());
			shopStatDao.saveEntity(shopStat);
		}
		
		LikeDetailEntity detailEntity = this.likeDetailService.getEntityByUserId(origin, likeDetail.getOuterId(), likeDetail.getUserId());
		if(null != detailEntity){
			detailEntity.setScore(1);
			//json.setMessage("Don't repeat it with same user {0}.", likeDetail.getUserId());
			if(null != userInfo){
				Controllers.prepareEntityForDataSaving(detailEntity, userInfo, false);
			}
			this.likeDetailService.updateEntity(detailEntity);
			this.shopStatDao.like(likeDetail.getOuterId());
			json.setMessage("1");
			json.setSuccess(true);
		} else {
			this.shopStatDao.like(likeDetail.getOuterId());
			likeDetail.setOrigin(origin);
			likeDetail.setScore(1);
			if(null != userInfo){
				likeDetail.setUserId(userInfo.getId());
				Controllers.prepareEntityForDataSaving(likeDetail, userInfo, false);
			}
			this.likeDetailService.saveEntity(likeDetail);
			json.setMessage("1");
			json.setSuccess(true);
		}
		return json;
	}
	
	public JsonResult unlike(LikeDetailEntity likeDetail, UserInfo userInfo,String origin) throws Exception{
		JsonResult json = new JsonResult();
		LikeDetailEntity detailEntity = this.likeDetailService.getEntityByUserId(
				origin, likeDetail.getOuterId(), likeDetail.getUserId());
		ShopStatEntity shopStat = shopStatDao.loadBySellerId(likeDetail.getOuterId());
		if(shopStat==null){
			shopStat = new ShopStatEntity();
			shopStat.setSellerId(likeDetail.getOuterId());
			shopStatDao.saveEntity(shopStat);
		}
		if(null != detailEntity){
			//json.setMessage("Don't repeat it with same ip address {0}.", likeDetail.getClientIp());
			detailEntity.setScore(-1);
			if(null != userInfo){
				Controllers.prepareEntityForDataSaving(detailEntity, userInfo, false);
			}
			this.likeDetailService.updateEntity(detailEntity);
			this.shopStatDao.unlike(likeDetail.getOuterId());
			json.setSuccess(true);
			json.setMessage("-1");
		} else {
			this.shopStatDao.unlike(likeDetail.getOuterId());
			likeDetail.setOrigin(origin);
			likeDetail.setScore(-1);
			if(null != userInfo){
				likeDetail.setUserId(userInfo.getId());
				Controllers.prepareEntityForDataSaving(likeDetail, userInfo, false);
			}
			this.likeDetailService.saveEntity(likeDetail);
			json.setMessage("-1");
			json.setSuccess(true);
		}
		return json;
	}

}
